/**
 * @(#)SelfKashangServiceImpl.java 2017年2月21日
 * Copyright(C) 2017 ZHEXIN IT CO.,LTD. All rights reserved.
 */
package org.jsbd.boss.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.jsbd.boss.dao.IUserClientInfoDAO;
import org.jsbd.boss.domian.DMClientInfo;
import org.jsbd.boss.domian.channel.QueryUserClientVo;
import org.jsbd.boss.domian.channel.SelfCardVo;
import org.jsbd.boss.domian.channel.ThirdUserQueryVo;
import org.jsbd.boss.domian.channel.UserClientInfo;
import org.jsbd.boss.enums.HistoryUserTableEnum;
import org.jsbd.boss.enums.LoginTypeEnum;
import org.jsbd.boss.service.ClientManager;
import org.jsbd.boss.service.IUserClientInfoService;
import org.jsbd.boss.util.BeanCopierUtil;
import org.jsbd.boss.util.CacheKey;
import org.jsbd.boss.util.HttpUtil;
import org.jsbd.boss.util.PropertiesUtil;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;

/**
 * 自有卡服务
 * 
 * @author <a href="mailto:xupengcheng@zhexinit.com" >徐鹏程</a>
 * @version 1.0.0
 */
@Service("selfKashangService")
public class SelfKashangServiceImpl extends ThirdLoginAdapterHandler<SelfCardVo> {

	@Resource
	private ClientManager clientManager;

	private final static String URL = PropertiesUtil.getValue("self_card_url");

	@Resource
	private IUserClientInfoService userClientInfoService;

	@Resource
	private IUserClientInfoDAO userClientInfoDAO;

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jsbd.boss.service.impl.AbsThirdUserServiceImpl#init()
	 */
	@Override
	public boolean init() {
		loginIndexKey = CacheKey.SELF_CARD_APK;
		loginType = LoginTypeEnum.SELF_CARD;
		queueKey = CacheKey.SELF_CARD_USER_QUEUE;
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.jsbd.boss.service.impl.AbsThirdUserServiceImpl#selectUserList(java
	 * .lang.Integer, java.lang.Integer, java.lang.Integer)
	 */
	@Override
	public List<SelfCardVo> selectUserList(ThirdUserQueryVo queryVo) {
		List<SelfCardVo> list = new ArrayList<>();
		try {
			String bizId = queryVo.getBizId();
			String url = URL + "?bizId=" + bizId;
			String response = HttpUtil.getInfo(url, 1000);
			if (StringUtils.isNotBlank(response)) {
				JSONObject responeObj = JSONObject.parseObject(response);
				String status = responeObj.getString("resultCode");
				if (StringUtils.isNotBlank(status) && status.equals("1")) {
					JSONObject data = responeObj.getJSONObject("data");
					if (data != null) {
						String imsi = data.getString("imsi");
						String phoneNum = data.getString("phoneNum");
						String iccid = data.getString("iccid");
						String tokenId = data.getString("tokenId");
						QueryUserClientVo userVo = new QueryUserClientVo();
						userVo.setProvince(queryVo.getProvinceId());
						UserClientInfo userClientInfo = userClientInfoDAO.selectRandomClientInfo(userVo);
						if (userClientInfo != null) {
							SelfCardVo cardVo = new SelfCardVo();
							BeanCopierUtil.UserClient2SelfCardVoBeanCopier.copy(userClientInfo, cardVo, null);
							cardVo.setImsi(imsi);
							cardVo.setImei(clientManager.initImei(imsi));
							cardVo.setIccid(iccid);
							cardVo.setMobile(phoneNum);
							cardVo.setTokenId(tokenId);
							cardVo.setId(0);
							cardVo.setBizId(bizId);
							cardVo.setSystime(String.valueOf(System.currentTimeMillis()));
							list.add(cardVo);
							userClientInfo.setImsi(imsi);
							userClientInfo.setImei(cardVo.getImei());
							userClientInfo.setIccid(iccid);
							userClientInfo.setPhoneNum(phoneNum);
							userClientInfo.setCarrier(ClientManager.checkImsi(imsi));
							userClientInfo.setTableName(HistoryUserTableEnum.HISTORY.getCode());
							userClientInfoService.addUserclient(userClientInfo);
							DMClientInfo clientInfo = new DMClientInfo();
							BeanCopierUtil.UserClientInfo2DMClientInfoBeanCopier.copy(userClientInfo, clientInfo, null);
							clientManager.recordNoSdkClient(clientInfo);
						}
					}
				}
			}
		} catch (Exception e) {
			LOGGER.error("获取手机号失败", e);
		}
		return list;
	}

}
